using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace hemsidan
{
	/// <summary>
	/// Summary description for blog.
	/// </summary>
	public partial class blog : Bases.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(!IsPostBack)
				LoadData();
		}


		private void LoadData()
		{
			this.myBlogRepeater.DataSource = Entities.Blog.GetRecent(15);
			this.myBlogRepeater.DataBind();
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.myBlogRepeater.ItemCreated += new RepeaterItemEventHandler(myBlogRepeater_ItemCreated);
		}
		#endregion

		private void myBlogRepeater_ItemCreated(object sender, RepeaterItemEventArgs e)
		{
			if (User.Identity.Name != string.Empty)
			{
				
                foreach (Control c in e.Item.Controls)
                    if (c.ID == "ToolBox")
                    {
                        c.Visible = true;
                        break;
                    }
			}
		}
        protected void myBlogRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "CreateBlogComment")
            {
                string Text = ((TextBox)e.Item.FindControl("CommentText")).Text;
                string CreatedBy = ((TextBox)e.Item.FindControl("CreatedBy")).Text;
                int BlogId = Convert.ToInt32(e.CommandArgument);

                hemsidan.Entities.BlogComment.Create(CreatedBy, Text, BlogId);
                LoadData();
            }
        }

        protected void BlogCommentItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "DeleteBlogComment")
            {
                int CommentId = Convert.ToInt32(e.CommandArgument);
                hemsidan.Entities.BlogComment.Delete(CommentId);
                LoadData();
            }
        }

        protected void BlogItemCreated(object source, RepeaterItemEventArgs e)
        {
            ((LinkButton)e.Item.FindControl("LB_Delete")).Visible = Context.User.Identity.IsAuthenticated;
        }
}
}
